package 算法.练习1;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * @author:谢君臣
 * @Date:2021/7/21-16:09
 * @version:1.0
 * @function:
 */
public class TestUnsafe {
    private static Integer value;
    private static long valueOffset;
    private static Unsafe unsafe = null;
    static {
        Field f = null;
        try {
            f = Unsafe.class.getDeclaredField("theUnsafe");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        f.setAccessible(true);
        try {
            unsafe = (Unsafe) f.get(null);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        try {
            valueOffset = unsafe.objectFieldOffset(TestUnsafe.class.getDeclaredField("value"));
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        value = 123;
        boolean bool = unsafe.compareAndSwapInt(value, valueOffset, 123, 1234);
        System.out.println(bool);
    }

}
